package cn.sise.edu.crm.service.impl;

import cn.sise.edu.crm.domain.Employee;
import cn.sise.edu.crm.mapper.EmployeeMapper;
import cn.sise.edu.crm.query.PageResult;
import cn.sise.edu.crm.query.QueryObject;
import cn.sise.edu.crm.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

/**
 * Created by chen on 2019/11/3.
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void insert(Employee employee, Long[] los) {
        employeeMapper.insert(employee);
        //维护关系
        if (los != null && los.length > 0) {
            for (Long roleId : los) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }

    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByEmployeeId(id);
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Employee employee, Long[] los) {
        employeeMapper.updateByPrimaryKey(employee);
        employeeMapper.deleteByEmployeeId(employee.getId());
        if (los != null) {
            for (Long lo : los) {
                employeeMapper.insertRelation(employee.getId(), lo);
            }
        }
    }

    @Override
    public Employee selectById(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        return new PageInfo<>(employeeMapper.selectForList(qo));
    }

    @Override
    public Employee login(String name, String password) {
       return employeeMapper.selectNameAndPassword(name,password);
    }

    @Override
    public void deleteByEmployeeIds(Long[] ids) {
        employeeMapper.deleteByEmployeeIdS(ids);
    }

    @Override
    public Employee selectByUsename(String username) {
        return employeeMapper.selectByUsename(username);
    }

    @Override
    public Workbook export() {
        List<Employee> employees = selectAll();

        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("sheet1");
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("名称");
        Cell cell1 = row.createCell(1);
        cell1.setCellValue("年龄");
        Cell cell2 = row.createCell(2);
        cell2.setCellValue("email");
        Cell cell3 = row.createCell(3);
        cell3.setCellValue("部门");
        for (int i = 1 ; i <= employees.size();i++){
            Row row1 = sheet.createRow(i);
            Employee employee = employees.get(i - 1);
            row1.createCell(0).setCellValue(employee.getName());
            row1.createCell(1).setCellValue(new Double(employee.getAge()));
            row1.createCell(2).setCellValue(employee.getEmail());
            if (employee.getDept() != null){
                row1.createCell(3).setCellValue(employee.getDept().getName());
            }
        }
        return wb;
    }

    @Override
    public void imports(InputStream inputStream) throws Exception {
        Workbook wb = WorkbookFactory.create(inputStream);
        Sheet sheet = wb.getSheetAt(0);
        //最后一行
        int lastRowIndex = sheet.getLastRowNum();
        for(int i = 1; i <= lastRowIndex; i++){
            Row row = sheet.getRow(i);

            Employee e = new Employee();
            String name = row.getCell(0).getStringCellValue();
            int age = new Double(row.getCell(1).getNumericCellValue()).intValue();
            String email = row.getCell(2).getStringCellValue();

            e.setName(name);
            e.setAge(age);
            e.setEmail(email);
            //默认的密码1
            e.setPassword(new SimpleHash("md5", "1", name).toString());
            //增加到数据库中
            employeeMapper.insert(e);
        }

    }

    @Override
    public Workbook temp() {
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("sheet1");
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("名称");
        Cell cell1 = row.createCell(1);
        cell1.setCellValue("年龄");
        Cell cell2 = row.createCell(2);
        cell2.setCellValue("email");
        return wb;
    }

    @Override
    public List<Employee> queryByRoleSn(String[] strings) {

        return employeeMapper.selectByRoleSn(strings);
    }
}
